package com.hz.sys.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.hz.utils.HttpClientUtil;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@EnableWebMvc
public class Swagger2 extends WebMvcConfigurerAdapter {
	@Value("${hz.project.versionUrl}")
    private String versionUrl;
	
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
	    registry.addResourceHandler("doc.html")
	    	.addResourceLocations("classpath:/META-INF/resources/");
	    registry.addResourceHandler("/webjars/**")
	    	.addResourceLocations("classpath:/META-INF/resources/webjars/");
	    super.addResourceHandlers(registry);
	}
	
	@Bean
    public Docket controllerApi() {
		String versionStr = HttpClientUtil.httpGetRequest(versionUrl);
		String[] versionArray = versionStr.split("[\n]");
		String version = versionArray[versionArray.length-1];
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title("标题：合智科技_demo_接口文档")
                        .description("描述：用于后续管理模块开发demo")
                        .contact(new Contact("liul",null,null))
                        .version("版本号:" + version)
                        .build())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.hz.ws"))
                .paths(PathSelectors.any())
                .build();
    }
	
	public String getVersionUrl() {
		return versionUrl;
	}

	public void setVersionUrl(String versionUrl) {
		this.versionUrl = versionUrl;
	}

}
